
export function createRouterMap(routes, pathMap) {
    pathMap = pathMap || {} // 扁平化的路由

    routes.forEach(route => {
        addRouteRecord(route, pathMap)

    })
    return { pathMap }
}

// 将用户的route属性 转为 {about, about/a}
function addRouteRecord(route, pathMap, parentRoute) {
    let path = parentRoute ? `${parentRoute.path === '/' ? '/' : `${parentRoute.path}/`}${route.path}` : route.path
    let record = {
        path,
        component: route.component,
        meta: route.meta,
        parent: parentRoute,

    }
    if (!pathMap[path]) {
        pathMap[path] = record
    }

    route.children?.forEach(childRoute => {
        addRouteRecord(childRoute, pathMap, record)
    })

}
